import bge

##
## This script controls the cameras used in the virtual environment. Cameras are
## set via the GUI, using global variables (bge.camera). This script makes sure the
## correct camera is active, and handles pan/tilt/zoom functions of each view. 
##

controller = bge.logic.getCurrentController()
owner = controller.owner
scene = bge.logic.getCurrentScene()

lmb = controller.sensors["LMB"]
mmb = controller.sensors["MMB"]
rmb = controller.sensors["RMB"]
Shift = controller.sensors["Shift"]
wheelUp = controller.sensors["WheelUp"]
wheelDown = controller.sensors["WheelDown"]
mousePos = controller.sensors["MousePos"]

rotCam = controller.actuators["rotCam"]
movCam = controller.actuators["moveCam"]
tiltCam = controller.actuators["tiltCam"]


# The script is used by multiple cameras, so use 'owner' to see which 
# camera to control

if owner.children[0].name == scene.active_camera.name and bge.suspend == False:
    dz = mousePos.position[0] - bge.mouse[0]
    dy = mousePos.position[1] - bge.mouse[1]

    if mmb.positive and Shift.positive == False:
        rotZ = float(dz / 100)
        rotY = float(dy / 100)
        rotCam.dRot = [0,0,-float(rotZ)]
        tiltCam.dRot = [-float(rotY),0,0]
        controller.activate(rotCam)
        controller.activate(tiltCam)    

    if not mmb.positive:
        controller.deactivate(rotCam)
        controller.deactivate(tiltCam)  
    
    if wheelDown.positive:
        owner.children[0].lens -= 1

    if wheelUp.positive:
        owner.children[0].lens += 1

    if Shift.positive == False:
        controller.deactivate(movCam) 

    if mmb.positive and Shift.positive:
        movCam.dLoc = [-dz/100,0,dy/100]
        controller.activate(movCam)

    bge.mouse[0] = mousePos.position[0]
    bge.mouse[1] = mousePos.position[1]


    width = bge.render.getWindowWidth()
    height = bge.render.getWindowHeight()
    
    camera = scene.active_camera
    left_1 = int(width/bge.SplitScreen); 
    bottom_1 = 0
    right_1 = width
    top_1 = height
    camera.setViewport(left_1, bottom_1, right_1, top_1)
    camera.useViewport = True
    camera.setOnTop()
    